package core;

import utils.QrCodeUtil;
import utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by jimmyhsu on 2016/12/18.
 */
public class StudentSignIn extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = StringUtils.getUtf8String(req.getParameter("username"));
        String password = StringUtils.getUtf8String(req.getParameter("password"));
        String qrcode = StringUtils.getUtf8String(req.getParameter("qrcode"));
        resp.setContentType("text/html;charset=gb2312");
        PrintWriter writer = resp.getWriter();
        if (StringUtils.isAnyoneEmpty(username, password, qrcode)) {
            writer.print("<script>alert('签到失败！','警告');location.href='../Student/signIn.jsp';</script>");
        } else {
            if (QrCodeUtil.signIn(qrcode, username, password)) {
                writer.print("<script>alert('签到成功！','提示');location.href='../Student/signIn.jsp';</script>");
            } else {
                writer.print("<script>alert('签到失败！','警告');location.href='../Student/signIn.jsp';</script>");
            }
        }
    }
}
